package cn.sxw.android.base.helper

import android.text.TextUtils
import android.widget.ImageView
import cn.sxw.android.R
import cn.sxw.android.base.task.LoadVideoFirstFrameInPathTask
import cn.sxw.android.base.utils.GlideStaticUtils

object LoadVideoFirstFrameHelper {
    fun loadNormal(ivThumb: ImageView, url: String) {
        load(ivThumb, url, false)
    }

    fun loadInRound(ivThumb: ImageView, url: String) {
        load(ivThumb, url, true)
    }

    private fun load(ivThumb: ImageView, url: String, rounded: Boolean) {
        // ++++++++++++++ 显示视频第一帧图片1 ++++++++++++++
        val cacheFirstFrame = LoadVideoFirstFrameInPathTask.getCacheFirstFrame(url)
        if (TextUtils.isEmpty(cacheFirstFrame)) { // 本地没有缓存
            LoadVideoFirstFrameInPathTask(object :
                LoadVideoFirstFrameInPathTask.Callback {
                override fun onPostExecute(filePath: String?) {
                    // 加载预览图
                    if (rounded)
                        GlideStaticUtils.displayRoundedImage(filePath, R.drawable.empty_photo, ivThumb)
                    else
                        GlideStaticUtils.displayImage(filePath, R.drawable.empty_photo, ivThumb)
                }
            }).execute(url)
        } else {// 已经存在缓存，直接加载
            // 加载预览图
            if (rounded)
                GlideStaticUtils.displayRoundedImage(cacheFirstFrame, R.drawable.empty_photo, ivThumb)
            else
                GlideStaticUtils.displayRoundedImage(cacheFirstFrame, R.drawable.empty_photo, ivThumb)
        }
        // ++++++++++++++ 显示视频第一帧图片2 ++++++++++++++
    }
}